import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/mvp/base_page_presenter.dart';
import 'package:flutter_deer/net/base_entity.dart';
import 'package:flutter_deer/net/net.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/setting/iview/account_iview.dart';
import 'package:flutter_deer/shop/models/user_entity.dart';


class AccountPagePresenter extends BasePagePresenter<AccountIMvpView> {

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      asyncRequestNetwork<BaseEntity<UserInfoEntity>>(Method.get,
        url: HttpApi.users,
        onSuccess: (data) {
          view.setUser(data?.data);
        },
        onError: (code,msg){

        }
      );
    });
  }

  Future<dynamic> getCurrentUserInfo() async {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      asyncRequestNetwork<BaseEntity<UserInfoEntity>>(Method.get,
          url: HttpApi.users,
          onSuccess: (data) {
            view.setUser(data?.data);
          },
          onError: (code,msg){

          }
      );
    });
  }

  Future<dynamic> updateAvatar(String? avatar) async {
      UserAvatarEntity userAvatarEntity = new UserAvatarEntity(username:SpUtil.getString(Constant.phone),avatar:avatar);
      await requestNetwork<OperatorBaseEntity>(Method.put,
        url: HttpApi.updateAvatar,
        params: userAvatarEntity.toJson(),
        onSuccess: (data) {
          view.uploadAvatarResult(data!.data);
        },
        onError: (code,msg){

        }
      );
  }

  Future<dynamic> updateNickname(String? nickname) async {
    UserEntity? user = UserEntity();
    user.username = SpUtil.getString(Constant.phone);
    user.nickname = nickname;
    await requestNetwork<OperatorBaseEntity>(Method.put,
        url: HttpApi.updateNickname,
        params: user.toJson(),
        onSuccess: (data) {
          view.setUpdateNicknameResult(data!.data);
        },
        onError: (code,msg){

        }
    );
  }

}
